/*
 * Class ItemSelect
 *
 * Version 1.0
 *
 * Friday, August 22, 2008
 *
 * Created by Palidino76
 */

package net.com.hydroPK.net.packethandler;

import net.com.hydroPK.Engine;
import net.com.hydroPK.model.Player;
import net.com.hydroPK.util.Misc;

public class ItemSelect implements Packet {
    /**
     * Handles certain item selecting such as eating and drinking.
     *
     * @param p          The Player which the frame should be handled for.
     * @param packetId   The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        int junk = p.stream.readUnsignedByte();
        int interfaceId = p.stream.readUnsignedWord();
        junk = p.stream.readUnsignedByte();
        int itemId = p.stream.readUnsignedWordBigEndian();
        int itemSlot = p.stream.readUnsignedWordA();
        p.attackingPlayer = false;
        if (itemSlot < 0 || itemSlot > p.items.length || p.items[itemSlot] != itemId) {
            return;
        }
        if (p.isDead || p.skillLvl[3] < 1) {
            return;
        }
        int lastHP = 0;
        if (interfaceId == 149) {

            int addID = 0;
	
            switch (itemId) {
            case 2452: //antifire potion (4)
            	addID = 2454;
            	break;
            case 2454: //antifire potion (3)
            	addID = 2456;
            	break;
            case 2456: //antifire potion (2)
            	addID = 2458;
            	break;
            case 2458: //antifire potion (1)
            	addID = 229;
            	break;
            case 9739: //combat potion -aka-overload
            	addID = 9741;
            	break;
            case 9741: //combat potion -aka-overload
            	addID = 9743;
            	break;
            case 9743: //combat potion -aka-overload
            	addID = 9745;
            	break;
            case 9745: //combat potion -aka-overload
            	addID = 229;
            	break;
                case 2428: //Attack
                    addID = 121;
                    break;
                case 121:
                    addID = 123;
                    break;
                case 123:
                    addID = 125;
                    break;
                case 125:
                    addID = 229;
                    break;

                case 113: //Strength
                    addID = 115;
                    break;
                case 115:
                    addID = 117;
                    break;
                case 117:
                    addID = 119;
                    break;
                case 119:
                    addID = 229;
                    break;

                case 2432: //Defence
                    addID = 133;
                    break;
                case 133:
                    addID = 135;
                    break;
                case 135:
                    addID = 137;
                    break;
                case 137:
                    addID = 229;
                    break;

                case 2436: //Super attack
                    addID = 145;
                    break;
                case 145:
                    addID = 147;
                    break;
                case 147:
                    addID = 149;
                    break;
                case 149:
                    addID = 229;
                    break;

                case 2440: //Super strength
                    addID = 157;
                    break;
                case 157:
                    addID = 159;
                    break;
                case 159:
                    addID = 161;
                    break;
                case 161:
                    addID = 229;
                    break;

                case 2442: //Super defence
                    addID = 163;
                    break;
                case 163:
                    addID = 165;
                    break;
                case 165:
                    addID = 167;
                    break;
                case 167:
                    addID = 229;
                    break;

                case 2444: //Ranging
                    addID = 169;
                    break;
                case 169:
                    addID = 171;
                    break;
                case 171:
                    addID = 173;
                    break;
                case 173:
                    addID = 229;
                    break;

                case 3040: //Magic
                    addID = 3042;
                    break;
                case 3042:
                    addID = 3044;
                    break;
                case 3044:
                    addID = 3046;
                    break;
                case 3046:
                    addID = 229;
                    break;

                case 2434: //Prayer
                    addID = 139;
                    break;
                case 139:
                    addID = 141;
                    break;
                case 141:
                    addID = 143;
                    break;
                case 143:
                    addID = 229;
                    break;

                case 2430: //Restore
                    addID = 127;
                    break;
                case 127:
                    addID = 129;
                    break;
                case 129:
                    addID = 131;
                    break;
                case 131:
                    addID = 229;
                    break;

                case 3024: //Super restore
                    addID = 3026;
                    break;
                case 3026:
                    addID = 3028;
                    break;
                case 3028:
                    addID = 3030;
                    break;
                case 3030:
                    addID = 229;
                    break;

                case 6685: //Saradomin brew
                    addID = 6687;
                    break;
                case 6687:
                    addID = 6689;
                    break;
                case 6689:
                    addID = 6691;
                    break;
                case 6691:
                    addID = 229;
                    break;

                case 2450: //Zamorak brew
                    addID = 189;
                    break;
                case 189:
                    addID = 191;
                    break;
                case 191:
                    addID = 193;
                    break;
                case 193:
                    addID = 229;
                    break;
		case 405:
		int[]Rewards = {6585,11694,14484,1333,4151,5680,6737,3751,10388,1149,1631,1305,241,1645,4587,1540,6889,8844,8845,8846,8847,8848,8847,8850,1075,1103,1139,1155,1173,1189,1127,1147,1201,1319,1289,2619,2625,2627,2621,2617,2615}; // Nice Rewards : Boaters, Headbands, Rune Stuff, Black & Addy G, T & H Stuff and a Dragon stuff
					int r = (int)(Math.random()*Rewards.length);
	Engine.playerItems.addItem(p, Rewards[r], 1);
                        Engine.playerItems.deleteItem(p, itemId, itemSlot, 1);


	break;
            case 1847:
	if (Engine.playerItems.hasPlayerItemAmount(p, 2705, 1)) {
	p.teleportTo(3557, 9703, 3, 4, 0, 8939, 8941, 8, 0, -1, 0);
	//p.teleportTo(3557, 9702, 3);//barrows sarcaphugas
	p.message("you uncrumple the note,");
	p.message("and a strange power comes over you");
	}
		break;
            case 2705:
	{
	    p.getActionSender().showInterface(p, 275);
                    p.getActionSender().setString(p, "Clue", 275, 2);
	    p.getActionSender().setString(p, "You have found this Clue Scroll from a Monster Drop", 275, 11);
	    p.getActionSender().setString(p, "To Complete this Clue Scroll, you must go through 3 parts.", 275, 12);
	    p.getActionSender().setString(p, "Your First Task is to do ::wt", 275, 13);
	    p.getActionSender().setString(p, "once there, you will find an unknown area.", 275, 14);
	    p.getActionSender().setString(p, "search the area, and you will find something.", 275, 15);
	    p.getActionSender().setString(p, "next, you use the item you find, and it takes you to another", 275, 16);
	    p.getActionSender().setString(p, "unknown area. Search this place, and you will find a ring.", 275, 17);
	    p.getActionSender().setString(p, "wear the ring, and then Operate it.", 275, 18);
	    p.getActionSender().setString(p, "You are somewhere never before seen.", 275, 19);
	    p.getActionSender().setString(p, "Go West, until you find a door that you can enter..", 275, 20);
	    p.getActionSender().setString(p, "once you find your door, search the house!", 275, 21);
	    p.getActionSender().setString(p, "your prize will be on the first floor!", 275, 22);
	    for (int i = 1; i<277; i++) {
	    	p.getActionSender().setString(p, "", 275, 23+i);
	    	}
	}
		break;
		case 1917: //beer to no beer
                    Engine.playerItems.deleteItem(p, 1917, 1);
                    Engine.playerItems.addItem(p, 1919, 1);
		    p.requestAnim(3040, 150);
			break;
		    
                case 11949:
                    p.getActionSender().showInterface(p, 659);
                    p.getActionSender().sendMessage(p, "You shake the snow globe.");
                    break;
                    //Potions

               

                case 10890: // Prayer Book
                    p.getActionSender().showInterface(p, 275);
                    p.getActionSender().setString(p, "Rune Resort Help", 275, 2);
                    p.getActionSender().setString(p, "<img=2>Main", 275, 11);
                    p.getActionSender().setString(p, "Rune Resort - Updated and a fresh start", 275, 12);
                    p.getActionSender().setString(p, "Coder/Hoster: <img=1>Dew<img=1>", 275, 13);
                    p.getActionSender().setString(p, "", 275, 14);
                    p.getActionSender().setString(p, "", 275, 15);
                    p.getActionSender().setString(p, "", 275, 16);
                    p.getActionSender().setString(p, "<img=2>Player Commands", 275, 17);
                    p.getActionSender().setString(p, "::help  --  Spawns a Book of knowledge", 275, 18);
                    p.getActionSender().setString(p, "::empty  --  Deletes all inventory items", 275, 19);
                    p.getActionSender().setString(p, "::clothes  --  Change your character's clothes", 275, 20);
                    p.getActionSender().setString(p, "::lunar  -- Sets the magic book to lunar magics", 275, 21);
                    p.getActionSender().setString(p, "::ancients  -- Sets the magic book to ancient magics", 275, 22);
                    p.getActionSender().setString(p, "::modern  -- Sets the magic book to modern magics", 275, 23);
                    p.getActionSender().setString(p, "::pkpoints  --  Tells you your pk points", 275, 24);
                    p.getActionSender().setString(p, "::changepass [password]  --  Changes your password", 275, 25);
                    p.getActionSender().setString(p, "::female  --  Change your character's gender to female", 275, 26);
                    p.getActionSender().setString(p, "::male  --  Change your character's gender to male", 275, 27);
                    p.getActionSender().setString(p, "Teles", 275, 28);
                    p.getActionSender().setString(p, "::sea - Sea Troll Queen ::train - Training", 275, 29);
                    p.getActionSender().setString(p, "", 275, 30);
                    p.getActionSender().setString(p, "<img=2>About", 275, 31);
                    p.getActionSender().setString(p, "For the best items in game, you should try gold farming", 275, 32);
                    p.getActionSender().setString(p, "on monsters, since they drop the best items, such as ", 275, 33);
                    p.getActionSender().setString(p, "the Barrows minigame or Godwars, they drop obvious", 275, 34);
                    p.getActionSender().setString(p, "items. Also Kbd, Corp, Kq, they all drop great items.", 275, 35);
                    p.getActionSender().setString(p, "", 275, 36);
                    p.getActionSender().setString(p, "You can train skills all around the server, and", 275, 37);
                    p.getActionSender().setString(p, "combat skills are fastest trained on Monsters ", 275, 38);
                    p.getActionSender().setString(p, "or Players, but the wilderness is dangerous!", 275, 39);
                    p.getActionSender().setString(p, "", 275, 40);
                    p.getActionSender().setString(p, "", 275, 41);
                    p.getActionSender().setString(p, "<img=2> Hope you have fun,", 275, 42);
                    p.getActionSender().setString(p, "<img=1> Rune Resort's Team", 275, 43);
                    for (int i = 1; i<256; i++) {
                    	p.getActionSender().setString(p, "", 275, 44+i);
                    	}
                    break;

                case 757: // Barrows book
                    p.getActionSender().showInterface(p, 275);
                    p.getActionSender().setString(p, "Barrows Brothers Guide", 275, 2);
                    p.getActionSender().setString(p, "The objective of this minigame is to kill all 6 assigned", 275, 11);
                    p.getActionSender().setString(p, "barrows brother.", 275, 12);
                    p.getActionSender().setString(p, "To start this minigame, Simply talk to Magnus Gram and", 275, 13);
                    p.getActionSender().setString(p, "say you want to start the minigame, then he'll teleport", 275, 14);
                    p.getActionSender().setString(p, "you to you're assigned brothers to kill. After you've", 275, 15);
                    p.getActionSender().setString(p, "killed the 6 assigned brothers, you will be teleported", 275, 16);
                    p.getActionSender().setString(p, " to the chest to get your reward. There are 25 lucky", 275, 17);
                    p.getActionSender().setString(p, "rewards you could possibly get, and that would be", 275, 18);
                    p.getActionSender().setString(p, "barrow armor and weapons.", 275, 19);
                    for (int i = 1; i<280; i++) {
                    	p.getActionSender().setString(p, "", 275, 20+i);
                    	}
                    break;

                    //Food

                case 373:
                case 379:
                case 385:
                case 391:
                case 3144:
                case 7060:
                case 10476:
                case 7946:
                    addID = -1;
                    break;

                    //Teletabs

                case 8007:
                    p.teletab("Varrock");
                    break;
                case 8008:
                    p.teletab("Lumbridge");
                    break;
                case 8013:
                    p.teletab("House");
                    break;
                case 8010:
                    p.teletab("Camelot");
                    break;
                case 8011:
                    p.teletab("Ardougne");
                    break;

                    //Bones

                case 526: //Bones
                    if (p.buryDelay <= 0) {
                        Engine.playerItems.deleteItem(p, itemId, itemSlot, 1);
                        p.requestAnim(827, 0);
                        p.appendExperience(400, 5);
                        p.buryDelay = 1;
                    }
                    break;

                case 532: //Big Bones
                    if (p.buryDelay <= 0) {
                        Engine.playerItems.deleteItem(p, itemId, itemSlot, 1);
                        p.requestAnim(827, 0);
                        p.appendExperience(650, 5);
                        p.buryDelay = 1;
                    }
                    break;

                case 536: //Dragon bones
                    if (p.buryDelay <= 0) {
                        Engine.playerItems.deleteItem(p, itemId, itemSlot, 1);
                        p.requestAnim(827, 0);
                        p.appendExperience(4500, 5);
                        p.buryDelay = 2;
                    }
                    break;

            }

            //Deleting

            if (addID != -1 && addID != 0) {
                if (p.drinkDelay == 0) {
                    Engine.playerItems.deleteItem(p, itemId, itemSlot, 1);
                    Engine.playerItems.addItem(p, addID, 1);
                    p.potion(itemId);
                }
            } else if (addID != 0) {
                if (itemId == 3144) {
                    if (p.drinkDelay == 0) {
                        Engine.playerItems.deleteItem(p, itemId, itemSlot, 1);
                        p.food(itemId);
                    }
                } else {
                    if (p.eatDelay == 0 && p.drinkDelay == 0) {
                        Engine.playerItems.deleteItem(p, itemId, itemSlot, 1);
                        p.food(itemId);
                    }
                }
            }

        } else {
            Misc.println("[" + p.username + "] Unhandled item select " + interfaceId + ":" + itemId);
        }
    }
}
